package com.salesmonitoring.accounts.view;

import javax.print.attribute.standard.JobName;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Font;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.border.Border;

import com.salesmonitoring.accounts.controller.AdminController;

@SuppressWarnings("serial")
public class AddUserUI extends JFrame implements ActionListener {

	private JPanel formPanel;
	private JLabel lblAU, lblUsername, lblPassword, lblConfirmPassword,
			lblName, lblRole, lblWarehouse, lblEmail, lblContactNo,
			lblRequired;
	private JTextField usernameTextField, contactNoTextField, nameTextField,
			emailTextField;

	private JPasswordField passwordField, confirmPasswordField;
	private JComboBox roleComboBox, warehouseComboBox;
	private JButton submitAddUserButton;
//	private N
	
	private String[] names, roles, warehouses;
	private ArrayList<Object> details;
	private boolean usernameFlag = false, passwordFlag = false, nameFlag = false, warehouseFlag = false, roleFlag = false, updateFlag = false;
	private String username = null, password = null, name = null, role = null, warehouse = null, email = null, contact = null;
	private AdminUI admin;

	public AddUserUI(AdminUI admin) {
		
		this.admin = admin;

		AdminController adminController = AdminController.getInstance();
		
		this.names = adminController.getAllAONames();
		this.roles = adminController.getAllRoles();
		this.warehouses = adminController.getAllWarehouses();
		
		initialize();
	}

	private void initialize() {

		setTitle("Bank of Makati | Add User");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 340, 395);
		

		formPanel = new JPanel();
		formPanel.setLayout(null);
		setContentPane(formPanel);

		lblAU = new JLabel("Add User");
		lblAU.setHorizontalAlignment(SwingConstants.CENTER);
		lblAU.setFont(new Font("Helvetica", Font.PLAIN, 20));
		lblAU.setBounds(6, 6, 324, 32);
		formPanel.add(lblAU);

		// ---Username 
		lblUsername = new JLabel("Username *");
		lblUsername.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblUsername.setBounds(6, 50, 95, 21);
		formPanel.add(lblUsername);

		usernameTextField = new JTextField();
		usernameTextField.setBounds(124, 45, 206, 28);
		formPanel.add(usernameTextField);
		usernameTextField.setColumns(10);

		// ---Password
		lblPassword = new JLabel("Password *");
		lblPassword.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblPassword.setBounds(6, 80, 95, 21);
		formPanel.add(lblPassword);

		passwordField = new JPasswordField();
		passwordField.setBounds(124, 75, 206, 28);
		formPanel.add(passwordField);

		// ---Confirm Password
		lblConfirmPassword = new JLabel("Confirm Password *");
		lblConfirmPassword.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblConfirmPassword.setBounds(6, 110, 135, 21);
		formPanel.add(lblConfirmPassword);

		confirmPasswordField = new JPasswordField();
		confirmPasswordField.setBounds(124, 105, 206, 28);
		formPanel.add(confirmPasswordField);

		// ---Name
		lblName = new JLabel("Name *");
		lblName.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblName.setBounds(6, 140, 95, 21);
		formPanel.add(lblName);

		nameTextField = new JTextField();
		nameTextField.setColumns(10);
		nameTextField.setBounds(124, 135, 206, 28);
		formPanel.add(nameTextField);

		// ---Role
		lblRole = new JLabel("Role *");
		lblRole.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblRole.setBounds(6, 170, 95, 21);
		formPanel.add(lblRole);

		roleComboBox = new JComboBox(roles);
		roleComboBox.setBounds(124, 167, 206, 27);
		roleComboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(roleComboBox.getSelectedItem().equals("account officer")) {
					warehouseComboBox.setEnabled(true);
					warehouseComboBox.removeAllItems();
					for(String s: warehouses) {
						warehouseComboBox.addItem(s);
					}
				} else {
					warehouseComboBox.removeAllItems();
					warehouseComboBox.setEnabled(false);
				}
				
			}
		});
		
		formPanel.add(roleComboBox);

		// ---Warehouse
		lblWarehouse = new JLabel("Warehouse");
		lblWarehouse.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblWarehouse.setBounds(6, 200, 95, 21);
		formPanel.add(lblWarehouse);

		warehouseComboBox = new JComboBox();
		warehouseComboBox.setEnabled(false);
		warehouseComboBox.setBounds(124, 197, 206, 27);
		formPanel.add(warehouseComboBox);

		// ---Email
		lblEmail = new JLabel("E-mail");
		lblEmail.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblEmail.setBounds(6, 230, 95, 21);
		formPanel.add(lblEmail);

		emailTextField = new JTextField();
		emailTextField.setColumns(10);
		emailTextField.setBounds(124, 225, 206, 28);
		formPanel.add(emailTextField);

		// ---Contact No
		lblContactNo = new JLabel("Contact No.");
		lblContactNo.setFont(new Font("Helvetica", Font.PLAIN, 13));
		lblContactNo.setBounds(6, 260, 95, 21);
		formPanel.add(lblContactNo);

		contactNoTextField = new JTextField();
		contactNoTextField.setColumns(10);
		contactNoTextField.setBounds(124, 255, 206, 28);
		formPanel.add(contactNoTextField);

		lblRequired = new JLabel("* Required Fields");
		lblRequired.setFont(new Font("Helvetica", Font.PLAIN, 10));
		lblRequired.setBounds(6, 346, 135, 16);
		formPanel.add(lblRequired);

		submitAddUserButton = new JButton("Submit");
		submitAddUserButton.setBounds(213, 305, 117, 29);
		submitAddUserButton.addActionListener(this);
		formPanel.add(submitAddUserButton);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		Border redBorder = BorderFactory.createEtchedBorder(Color.WHITE, Color.RED);
		Border defaultBorder = BorderFactory.createEtchedBorder(Color.WHITE, Color.GRAY);
		
		if(usernameTextField.getText().equals("")) {
			usernameTextField.setBorder(redBorder);
			usernameTextField.requestFocus();
			usernameFlag = false;
		} else {
			usernameTextField.setBorder(defaultBorder);
			usernameFlag = true;
		}
		
		if(passwordField.getText().equals("") || confirmPasswordField.getText().equals("") || 
				!passwordField.getText().equals(confirmPasswordField.getText())) {
			passwordField.setBorder(redBorder);
			passwordField.setText("");
			confirmPasswordField.setText("");
			passwordField.requestFocus();
			passwordFlag = false;
		} else {
			passwordField.setBorder(defaultBorder);
			passwordFlag = true;
		}
		
		if(nameTextField.getText().equals("")) {
			nameTextField.setBorder(redBorder);
			nameTextField.requestFocus();
			nameFlag = false;
		} else {
			nameTextField.setBorder(defaultBorder);
			nameFlag = true;
		}
		
		if(roleComboBox.getSelectedItem().equals("")) {
			roleComboBox.setBorder(redBorder);
			roleFlag = false;
		} else {
			roleComboBox.setBorder(defaultBorder);
			roleFlag = true;
		}
		
		if(roleComboBox.getSelectedItem().equals("account officer") && warehouseComboBox.getSelectedItem().equals("")) {
			warehouseComboBox.setBorder(redBorder);
			warehouseFlag = false;
		} else {
			warehouseComboBox.setBorder(defaultBorder);
			warehouseFlag = true;
		}
		
		if(usernameFlag && passwordFlag && nameFlag && warehouseFlag && roleFlag) {
			
			details = new ArrayList<Object>();
			
			details.add(usernameTextField.getText());
			details.add(passwordField.getText());
			details.add(nameTextField.getText());
			details.add(roleComboBox.getSelectedItem().toString());
			details.add((warehouseComboBox.getSelectedItem() != null) ? warehouseComboBox.getSelectedItem().toString() : null);
			details.add(emailTextField.getText());
			
			try {
				details.add((contactNoTextField.getText() != null) ? Integer.parseInt(contactNoTextField.getText()) : 0);
			} catch(Exception e) {
				JOptionPane.showMessageDialog(null, "Invalid contact number.");
				return;
			}
			
			AdminController adminController = AdminController.getInstance();
			int status = adminController.addUser(details);
			
			if(status == 1) {
				updateFlag = true;
			} else {
				updateFlag = false;
			}
			
			if(updateFlag != false) {
				admin.updateAccountsTable();
			}
			
			if(status == 1) {
				JOptionPane.showMessageDialog(null, "User successfully added.");
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(null, "Error in adding user.");
			}
			
		} else {
			
			JOptionPane.showMessageDialog(null, "Please fill out required fields.");
		}
	}
}